<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/Typescript/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/Typescript/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/Typescript/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/Typescript/images/logo.svg" color="#222">

<link rel="stylesheet" href="/Typescript/css/main.css">


<link rel="stylesheet" href="/Typescript/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/Typescript/","scheme":"Muse","version":"7.7.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="类定义: 类是面向对象语言的一种特定的类型，是es6开始在javascript中出现 tsconfig.json中,有一个选项是 strictPropertyInitialization, 表示启用类属性初始化的严格检查  定义一个类 123456class Animal &amp;#123;    name: string;    constructor(name: string) &amp;#123;">
<meta property="og:type" content="article">
<meta property="og:title" content="Typescript&#x2F;类">
<meta property="og:url" content="http://yoursite.com/2020/11/21/Typescript/%E7%B1%BB/index.html">
<meta property="og:site_name" content="Typescript">
<meta property="og:description" content="类定义: 类是面向对象语言的一种特定的类型，是es6开始在javascript中出现 tsconfig.json中,有一个选项是 strictPropertyInitialization, 表示启用类属性初始化的严格检查  定义一个类 123456class Animal &amp;#123;    name: string;    constructor(name: string) &amp;#123;">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-11-21T03:51:29.552Z">
<meta property="article:modified_time" content="2020-11-21T03:51:29.552Z">
<meta property="article:author" content="HC">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://yoursite.com/2020/11/21/Typescript/%E7%B1%BB/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>

  <title>Typescript/类 | Typescript</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <div>
      <a href="/Typescript/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Typescript</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/Typescript/" rel="section"><i class="fa fa-fw fa-home"></i>Home</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/Typescript/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>Archives</a>

  </li>
  </ul>

</nav>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="en">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/11/21/Typescript/%E7%B1%BB/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/Typescript/images/avatar.gif">
      <meta itemprop="name" content="HC">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Typescript">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Typescript/类
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2020-11-21 11:51:29" itemprop="dateCreated datePublished" datetime="2020-11-21T11:51:29+08:00">2020-11-21</time>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="类"><a href="#类" class="headerlink" title="类"></a>类</h1><p>定义: 类是面向对象语言的一种特定的类型，是es6开始在javascript中出现</p>
<p>tsconfig.json中,有一个选项是 strictPropertyInitialization, 表示启用类属性初始化的严格检查</p>
<ul>
<li><p>定义一个类</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> Animal &#123;</span><br><span class="line">    name: <span class="built_in">string</span>;</span><br><span class="line">    <span class="keyword">constructor</span>(<span class="params">name: <span class="built_in">string</span></span>) &#123;</span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>存取器</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> Animal &#123;</span><br><span class="line">    name: <span class="built_in">string</span>;</span><br><span class="line">    age: <span class="built_in">number</span>;</span><br><span class="line">    <span class="keyword">constructor</span>(<span class="params">name: <span class="built_in">string</span>, age: <span class="built_in">number</span></span>) &#123;</span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">        <span class="keyword">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">get</span> iAge() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.age;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">set</span>(newAge: <span class="built_in">number</span>) &#123;</span><br><span class="line">        <span class="keyword">this</span>.age = newAge;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(animal.iAge);<span class="comment">// 18</span></span><br><span class="line">animal.iAge = <span class="number">20</span>;</span><br><span class="line"><span class="built_in">console</span>.log(animal.iAge);<span class="comment">// 20</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>参数属性</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> Animal &#123;</span><br><span class="line">    <span class="comment">// 会自动成为类型属性</span></span><br><span class="line">    <span class="keyword">constructor</span>(<span class="params"><span class="keyword">public</span> iName: <span class="built_in">string</span></span>) &#123;</span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">get</span> name() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.iName;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">set</span> name(newName: <span class="built_in">string</span>) &#123;</span><br><span class="line">        <span class="keyword">this</span>.iName = newName</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> animal = <span class="keyword">new</span> Animal(<span class="string">'pig'</span>);</span><br><span class="line"><span class="built_in">console</span>.log(animal.name);</span><br><span class="line">animal.name = <span class="string">'cat'</span>;</span><br><span class="line"><span class="built_in">console</span>.log(animal.name);</span><br></pre></td></tr></table></figure>
</li>
<li><p>readonly 修饰符</p>
<p>readonly在编译时进行检查，const在运行是检查</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> Animal &#123;</span><br><span class="line">    <span class="keyword">public</span> readonly name: <span class="built_in">string</span> = <span class="string">'hchlq'</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> animal = <span class="keyword">new</span> Animal();</span><br><span class="line"><span class="built_in">console</span>.log(animal.name);</span><br><span class="line">animal.name = <span class="string">'hc'</span>;<span class="comment">// 报错</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>继承</p>
<ul>
<li>子类继承父类后子类的实例就拥有了父类中的属性和方法，可以增强代码的可复用性</li>
<li>将子类公有 的方法抽象到父类中去，自己的逻辑可以重写父类的该方法</li>
<li>super可以调用父类的属性和方法</li>
</ul>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> Person &#123;</span><br><span class="line">    <span class="keyword">public</span> name: <span class="built_in">string</span>;</span><br><span class="line">    <span class="keyword">public</span> age: <span class="built_in">number</span>;</span><br><span class="line">    <span class="keyword">constructor</span>(<span class="params">name: <span class="built_in">string</span>, age: <span class="built_in">number</span></span>) &#123;</span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">        <span class="keyword">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line">    getName():<span class="built_in">string</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">'abd'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">class</span> Student <span class="keyword">extends</span> Person &#123;</span><br><span class="line">    <span class="keyword">public</span> sex: <span class="built_in">string</span>;</span><br><span class="line">    <span class="keyword">constructor</span>(<span class="params">name:<span class="built_in">string</span>, age: <span class="built_in">number</span>, sex: <span class="built_in">string</span></span>) &#123;</span><br><span class="line">        <span class="keyword">super</span>(name, age);</span><br><span class="line">        <span class="keyword">this</span>.sex = sex;</span><br><span class="line">        <span class="keyword">super</span>.getName();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> student = <span class="keyword">new</span> Student(<span class="string">'hchlq'</span>, <span class="number">18</span>, <span class="string">'male'</span>);</span><br><span class="line"><span class="built_in">console</span>.log(student.name, student.age, student.sex);</span><br></pre></td></tr></table></figure>
</li>
<li><p>类中的修饰符</p>
<ul>
<li><p>public 公有的  自己，子类和外部实例都可以访问</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> Animal &#123;</span><br><span class="line">    <span class="keyword">public</span> name: <span class="built_in">string</span> = <span class="string">'hchlq'</span>;</span><br><span class="line">    getName() &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="keyword">this</span>.name);<span class="comment">// hchlq</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">class</span> Cat <span class="keyword">extends</span> Animal &#123;</span><br><span class="line">    getFatherName() &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="keyword">this</span>.name);<span class="comment">// hchlq</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> animal = <span class="keyword">new</span> Animal();</span><br><span class="line"><span class="keyword">const</span> cat = <span class="keyword">new</span> Cat();</span><br><span class="line"><span class="built_in">console</span>.log(animal.name);<span class="comment">// hchlq</span></span><br><span class="line">animal.getName();</span><br></pre></td></tr></table></figure>
</li>
<li><p>protected 保护的  只有自己和子类可以访问</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> Animal &#123;</span><br><span class="line">    <span class="keyword">protected</span> name: <span class="built_in">string</span> = <span class="string">'hchlq'</span>;</span><br><span class="line">    getName() &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="keyword">this</span>.name);<span class="comment">// hchlq</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">class</span> Cat <span class="keyword">extends</span> Animal &#123;</span><br><span class="line">    getFatherName() &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="keyword">this</span>.name);<span class="comment">// hchlq</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> animal = <span class="keyword">new</span> Animal();</span><br><span class="line"><span class="keyword">const</span> cat = <span class="keyword">new</span> Cat();</span><br><span class="line"><span class="built_in">console</span>.log(animal.name);<span class="comment">// 报错</span></span><br><span class="line">animal.getName();</span><br></pre></td></tr></table></figure>
</li>
<li><p>private  私有的  只有自己可以访问</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> Animal &#123;</span><br><span class="line">    <span class="keyword">private</span> name: <span class="built_in">string</span> = <span class="string">'hchlq'</span>;</span><br><span class="line">    getName() &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="keyword">this</span>.name);<span class="comment">// hchlq</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">class</span> Cat <span class="keyword">extends</span> Animal &#123;</span><br><span class="line">    getFatherName() &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="keyword">this</span>.name);<span class="comment">// 报错</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> animal = <span class="keyword">new</span> Animal();</span><br><span class="line"><span class="keyword">const</span> cat = <span class="keyword">new</span> Cat();</span><br><span class="line"><span class="built_in">console</span>.log(animal.name);<span class="comment">// 报错</span></span><br><span class="line">animal.getName();</span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
<li><p>类上的静态属性和静态方法</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> Animal &#123;</span><br><span class="line">    <span class="keyword">static</span> myName: <span class="built_in">string</span> = <span class="string">'cat'</span>;</span><br><span class="line">    <span class="keyword">static</span> getName(name: <span class="built_in">string</span>):<span class="built_in">string</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(Animal.myName);</span><br><span class="line"><span class="keyword">const</span> r = Animal.getName(<span class="string">'hchlq'</span>);</span><br><span class="line"><span class="built_in">console</span>.log(r);</span><br></pre></td></tr></table></figure>
</li>
<li><p>装饰器</p>
<p><strong>装饰器只能修饰类</strong></p>
<ul>
<li>装饰器是一种特殊类型的声明， 它能被附加到类声明，方法，属性和参数上，修改类的行为</li>
<li>常见的装饰器有类装饰器，属性装饰器，方法装饰器和参数装饰器</li>
<li>装饰器的写法分为普通装饰器和装饰器工厂</li>
</ul>
</li>
</ul>
<ul>
<li><p>普通类装饰器</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 普通装饰器</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">addMethod</span>(<span class="params">Ctor: <span class="built_in">Function</span></span>) </span>&#123;</span><br><span class="line">    Ctor.prototype.age = <span class="number">18</span>;</span><br><span class="line">    Ctor.prototype.getName = ():<span class="function"><span class="params">string</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">'abac'</span>;</span><br><span class="line">    &#125; </span><br><span class="line">&#125;</span><br><span class="line"><span class="meta">@addMethod</span></span><br><span class="line"><span class="keyword">class</span> Animal1 &#123;</span><br><span class="line">    <span class="keyword">public</span> age!: <span class="built_in">number</span>;</span><br><span class="line">    <span class="keyword">public</span> getName!: <span class="function"><span class="params">()</span> =&gt;</span> <span class="built_in">string</span>;</span><br><span class="line">    <span class="keyword">public</span> name:<span class="built_in">string</span> = <span class="string">'hchlq'</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> animal = <span class="keyword">new</span> Animal1();</span><br><span class="line"><span class="built_in">console</span>.log(animal.name);</span><br><span class="line"><span class="built_in">console</span>.log(animal.age);</span><br><span class="line"><span class="built_in">console</span>.log(animal.getName());</span><br></pre></td></tr></table></figure>
</li>
<li><p>工厂装饰器</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">/ 装饰器工厂</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">addMethodFac</span>(<span class="params">msg: <span class="built_in">string</span></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(msg);</span><br><span class="line">    <span class="keyword">return</span> <span class="function">(<span class="params">Ctor: <span class="built_in">Function</span></span>) =&gt;</span> &#123;</span><br><span class="line">        Ctor.prototype.age = <span class="number">18</span>;</span><br><span class="line">        Ctor.prototype.getName = ():<span class="function"><span class="params">string</span> =&gt;</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="string">'abac'</span>;</span><br><span class="line">        &#125; </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="meta">@addMethodFac</span>(<span class="string">'hchlq'</span>)</span><br><span class="line"><span class="keyword">class</span> Animal1 &#123;</span><br><span class="line">    <span class="keyword">public</span> age!: <span class="built_in">number</span>;</span><br><span class="line">    <span class="keyword">public</span> getName!: <span class="function"><span class="params">()</span> =&gt;</span> <span class="built_in">string</span>;</span><br><span class="line">    <span class="keyword">public</span> name:<span class="built_in">string</span> = <span class="string">'hchlq'</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> animal = <span class="keyword">new</span> Animal1();</span><br><span class="line"><span class="built_in">console</span>.log(animal.name);</span><br><span class="line"><span class="built_in">console</span>.log(animal.age);</span><br><span class="line"><span class="built_in">console</span>.log(animal.getName());</span><br></pre></td></tr></table></figure>
</li>
<li><p>替换类装饰器</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 还可以替换类</span></span><br><span class="line"><span class="comment">// 返回的类结构要和传入的类的结构一样</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">addMethodFac</span>(<span class="params">constructor: <span class="built_in">Function</span></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">class</span> &#123;</span><br><span class="line">        <span class="keyword">public</span> age!: <span class="built_in">number</span>;</span><br><span class="line">        <span class="keyword">public</span> getName!: <span class="function"><span class="params">()</span> =&gt;</span> <span class="built_in">string</span>;</span><br><span class="line">        <span class="keyword">public</span> name: <span class="built_in">string</span> = <span class="string">'abc'</span>;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="meta">@addMethodFac</span></span><br><span class="line"><span class="keyword">class</span> Animal1 &#123;</span><br><span class="line">    <span class="keyword">public</span> age!: <span class="built_in">number</span>;</span><br><span class="line">    <span class="keyword">public</span> getName!: <span class="function"><span class="params">()</span> =&gt;</span> <span class="built_in">string</span>;</span><br><span class="line">    <span class="keyword">public</span> name: <span class="built_in">string</span> = <span class="string">'hchlq'</span>;ts</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> animal = <span class="keyword">new</span> Animal1();</span><br><span class="line"><span class="built_in">console</span>.log(animal.name);</span><br></pre></td></tr></table></figure>
</li>
<li><p>属性装饰器</p>
<ul>
<li>有两个参数<ul>
<li>第一个参数对于静态成员是类的构造函数，对实例成员是类的原型对象</li>
<li>第二个参数是属性的名字</li>
</ul>
</li>
</ul>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">type</span> ProPertyType = <span class="built_in">Function</span> | object;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">enhanceProperty</span>(<span class="params">ctor: <span class="built_in">any</span>, name: <span class="built_in">string</span></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> tempName: <span class="built_in">string</span> = ctor[name];</span><br><span class="line">    <span class="built_in">Object</span>.defineProperty(ctor, name, &#123;</span><br><span class="line">        <span class="keyword">get</span>(): <span class="built_in">string</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> tempName.toUpperCase();</span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="keyword">set</span>(newName: <span class="built_in">string</span>) &#123;</span><br><span class="line">            tempName = newName;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">class</span> Animal1 &#123;</span><br><span class="line">    <span class="meta">@enhanceProperty</span></span><br><span class="line">    <span class="keyword">static</span> sName: <span class="built_in">string</span> = <span class="string">'hc'</span>;</span><br><span class="line">    <span class="meta">@enhanceProperty</span></span><br><span class="line">    <span class="keyword">public</span> name: <span class="built_in">string</span> = <span class="string">'hchlq'</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> animal = <span class="keyword">new</span> Animal1();</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(animal.name, Animal1.sName);</span><br></pre></td></tr></table></figure>
</li>
<li><p>方法装饰器</p>
<ul>
<li>三个参数<ul>
<li>第一个参数: 如果是静态方法，那么就是类本身，如果是实例方法，那么就是类的原型对象</li>
<li>第二个参数: 是方法的名字</li>
<li>第三个是方法描述符</li>
</ul>
</li>
</ul>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">enhanceMethod</span>(<span class="params">ctor: <span class="built_in">any</span>, name: <span class="built_in">string</span>, descriptor: PropertyDescriptor</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(ctor, name, descriptor);</span><br><span class="line">    descriptor.value();</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">class</span> Animal1 &#123;</span><br><span class="line">    <span class="keyword">public</span> name: <span class="built_in">string</span> = <span class="string">'hchlq'</span>;</span><br><span class="line">    <span class="meta">@enhanceMethod</span></span><br><span class="line">    getName(): <span class="built_in">string</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">'hchlq'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> animal = <span class="keyword">new</span> Animal1();</span><br></pre></td></tr></table></figure>
</li>
<li><p>参数装饰器</p>
<ul>
<li>三个参数<ul>
<li>第一个参数: 如果是静态成员是类的构造函数，对于实例成员是类的原型对象</li>
<li>第二个参数: 参数的名字</li>
<li>第三个参数: 参数在参数列表的索引</li>
</ul>
</li>
</ul>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">enhanceParam</span>(<span class="params">ctor: <span class="built_in">any</span>, name: <span class="built_in">string</span>, index:<span class="built_in">number</span></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(ctor, name, index);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">class</span> Animal1 &#123;</span><br><span class="line">    <span class="keyword">public</span> name: <span class="built_in">string</span> = <span class="string">'hchlq'</span>;</span><br><span class="line">    getName(age: <span class="built_in">number</span>, <span class="meta">@enhanceParam</span> sex: <span class="built_in">string</span>): <span class="built_in">string</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.name;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>装饰器执行顺序</p>
<ol>
<li>参数装饰器   多个参数的顺序是: 从最后一个参数开始</li>
<li>方法装饰器  多个装饰器对一个方法时，顺序是从下到上 (谁先靠近方法，谁先执行)</li>
<li>类装饰器  多个装饰器对一个类时, 顺序是从下到上  (谁先靠近类，谁先执行)</li>
</ol>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">enhanceMethod1</span>(<span class="params">ctor: <span class="built_in">any</span>, name: <span class="built_in">string</span>, descriptor: PropertyDescriptor</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'方法装饰器1'</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">enhanceMethod2</span>(<span class="params">ctor: <span class="built_in">any</span>, name: <span class="built_in">string</span>, descriptor: PropertyDescriptor</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'方法装饰器2'</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">enhanceParam1</span>(<span class="params">ctor: <span class="built_in">any</span>, name: <span class="built_in">string</span>, index: <span class="built_in">number</span></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'参数装饰器1'</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">enhanceParam2</span>(<span class="params">ctor: <span class="built_in">any</span>, name: <span class="built_in">string</span>, index: <span class="built_in">number</span></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'参数装饰器2'</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">addMethod2</span>(<span class="params">Ctor: <span class="built_in">Function</span></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'类装饰器2'</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">addMethod1</span>(<span class="params">Ctor: <span class="built_in">Function</span></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'类装饰器1'</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="meta">@addMethod1</span></span><br><span class="line"><span class="meta">@addMethod2</span></span><br><span class="line"><span class="keyword">class</span> Animal1 &#123;</span><br><span class="line">    <span class="meta">@enhanceProperty</span></span><br><span class="line">    <span class="keyword">public</span> name: <span class="built_in">string</span> = <span class="string">'hchlq'</span>;</span><br><span class="line">    <span class="meta">@enhanceMethod1</span></span><br><span class="line">    <span class="meta">@enhanceMethod2</span></span><br><span class="line">    getName(<span class="meta">@enhanceParam1</span> age: <span class="built_in">number</span>, <span class="meta">@enhanceParam2</span> sex: <span class="built_in">string</span>): <span class="built_in">string</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.name;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> animal = <span class="keyword">new</span> Animal1();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 输出</span></span><br><span class="line">参数装饰器<span class="number">2</span></span><br><span class="line">参数装饰器<span class="number">1</span></span><br><span class="line">方法装饰器<span class="number">2</span></span><br><span class="line">方法装饰器<span class="number">1</span></span><br><span class="line">类装饰器<span class="number">2</span></span><br><span class="line">类装饰器<span class="number">1</span></span><br></pre></td></tr></table></figure>

</li>
</ul>
<ul>
<li><p>抽象类</p>
<ul>
<li>抽象描述一种抽象的概念，无法被实例化，只能被继承</li>
<li>无法创建抽象类的实例</li>
<li>抽象方法不能在抽象类中实现，只能在抽象类的子类中去实现，并且是必须实现</li>
</ul>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> &#123;&#125;;</span><br><span class="line"><span class="keyword">abstract</span> <span class="keyword">class</span> Person &#123;</span><br><span class="line">    name!: <span class="built_in">string</span>;</span><br><span class="line">    <span class="keyword">abstract</span> speak():<span class="built_in">string</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">class</span> Teacher <span class="keyword">extends</span> Person &#123;</span><br><span class="line">    <span class="keyword">public</span> name: <span class="built_in">string</span> = <span class="string">'hchlq'</span>;</span><br><span class="line">    speak():<span class="built_in">string</span> &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'--'</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="string">'abc'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>抽象方法</p>
<ul>
<li>必须出现在抽象类中</li>
<li>如果抽象类中的方法不加abstract修饰，那么必须在抽象类中实现</li>
<li>如果抽象类中加了abstract修饰，那么必须在子类中去实现，并且必须要实现</li>
<li>子类可以对抽象方法进行不同的实现</li>
</ul>
</li>
<li><p>与类有关的修饰符</p>
<ul>
<li>访问控制修饰符<ul>
<li>public</li>
<li>protected</li>
<li>private</li>
</ul>
</li>
<li>静态属性<ul>
<li>static</li>
</ul>
</li>
<li>抽象类、抽象方法<ul>
<li>abstract</li>
</ul>
</li>
</ul>
</li>
<li><p>重写和重载的区别</p>
<ul>
<li><p>重写是指子类对父类的方法进行重写</p>
</li>
<li><p>重载是指为同一个函数提供多个类型定义</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 重写</span></span><br><span class="line"><span class="keyword">class</span> Animal &#123;</span><br><span class="line">    speak():<span class="built_in">void</span> &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'动物'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> Cat <span class="keyword">extends</span> Animal &#123;</span><br><span class="line">    speak():<span class="built_in">void</span> &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'猫'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> Dog <span class="keyword">extends</span> Animal &#123;</span><br><span class="line">    speak():<span class="built_in">void</span> &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'狗'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 重载</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fn</span>(<span class="params">name: <span class="built_in">string</span></span>):<span class="title">void</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fn</span>(<span class="params">name: <span class="built_in">number</span></span>):<span class="title">void</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fn</span>(<span class="params">name: <span class="built_in">any</span></span>):<span class="title">void</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">typeof</span> name === <span class="string">'string'</span>) &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'string'</span>);</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="keyword">typeof</span> name === <span class="string">'number'</span>) &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'number'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
<li><p>继承和多态</p>
<ul>
<li><p>继承: 子类继承父类，子类除了拥有父类的所有特性之外，还可以实现自己的特性</p>
</li>
<li><p>多态: 由继承产生了相关了不同的类，对同一个方法有不同的行为</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> Home &#123;</span><br><span class="line">    speak():<span class="built_in">void</span> &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'aaa'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">class</span> ClassRoom <span class="keyword">extends</span> Home &#123;</span><br><span class="line">    speak():<span class="built_in">void</span> &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'class Room'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">class</span> Desk <span class="keyword">extends</span> ClassRoom &#123;</span><br><span class="line">    speak():<span class="built_in">void</span> &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'desk'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> cRoom = <span class="keyword">new</span> ClassRoom();</span><br><span class="line"><span class="keyword">const</span> desk = <span class="keyword">new</span> Desk();</span><br><span class="line"></span><br><span class="line">cRoom.speak();</span><br><span class="line">desk.speak();</span><br></pre></td></tr></table></figure>

</li>
</ul>
</li>
</ul>

    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/Typescript/2020/11/21/Typescript/%E6%B3%9B%E5%9E%8B/" rel="prev" title="Typescript/泛型">
      <i class="fa fa-chevron-left"></i> Typescript/泛型
    </a></div>
      <div class="post-nav-item">
    <a href="/Typescript/2020/11/21/Typescript/%E7%B1%BB%E5%9E%8B%E4%BF%9D%E6%8A%A4/" rel="next" title="Typescript/类型保护">
      Typescript/类型保护 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#类"><span class="nav-number">1.</span> <span class="nav-text">类</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">HC</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/Typescript/archives/">
        
          <span class="site-state-item-count">12</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">HC</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> v4.2.1
  </div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">Theme – <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> v7.7.2
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/Typescript/lib/anime.min.js"></script>
  <script src="/Typescript/lib/velocity/velocity.min.js"></script>
  <script src="/Typescript/lib/velocity/velocity.ui.min.js"></script>

<script src="/Typescript/js/utils.js"></script>

<script src="/Typescript/js/motion.js"></script>


<script src="/Typescript/js/schemes/muse.js"></script>


<script src="/Typescript/js/next-boot.js"></script>




  















  

  

</body>
</html>
